package com.io.controller;

import com.io.dto.ApiResult;
import com.io.entity.SysRoleEntity;
import com.io.exception.SelfException;
import com.io.service.SysRoleDeptService;
import com.io.service.SysRoleMenuService;
import com.io.service.SysRoleService;
import com.io.utils.PageUtils;
import com.io.utils.annotation.DataFilter;
import com.io.utils.annotation.SysLog;
import com.io.utils.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 * @author guodd
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends BaseController {
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysRoleDeptService sysRoleDeptService;

    @ApiOperation("角色列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:role:list")
    @DataFilter(subDept = true, user = false)
    public ApiResult list(@RequestParam Map<String, Object> params) {
        PageUtils page = sysRoleService.queryPage(params);
        return ApiResult.ok(page.getList()).put("total", page.getTotalCount());
    }

    @GetMapping("/select")
    @RequiresPermissions("sys:role:select")
    public ApiResult select() {
        List<SysRoleEntity> list = sysRoleService.list();

        return ApiResult.ok().put("list", list);
    }

    @ApiOperation("角色信息")
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
    public ApiResult info(@PathVariable("roleId") Long roleId) {
        SysRoleEntity role = sysRoleService.getById(roleId);
        if (StringUtils.isEmpty(role)) {
            throw new SelfException("该角色不存在！");
        }

        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
        role.setMenuIdList(menuIdList);

        //查询角色对应的部门
        List<Long> deptIdList = sysRoleDeptService.queryDeptIdList(new Long[]{roleId});
        role.setDeptIdList(deptIdList);

        return ApiResult.ok().put("role", role);
    }

    @ApiOperation("保存角色")
    @SysLog("保存角色")
    @RequestMapping("/save")
    @RequiresPermissions("sys:role:save")
    public ApiResult save(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);
        sysRoleService.saveRole(role);
        return ApiResult.ok();
    }

    @ApiOperation("修改角色")
    @SysLog("修改角色")
    @PostMapping("/update")
    @RequiresPermissions("sys:role:update")
    public ApiResult update(@RequestBody SysRoleEntity role) {
        ValidatorUtils.validateEntity(role);
        sysRoleService.update(role);
        return ApiResult.ok();
    }

    @ApiOperation("删除角色")
    @SysLog("删除角色")
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public ApiResult delete(@RequestBody Long[] roleIds) {
        sysRoleService.deleteBatch(roleIds);
        return ApiResult.ok();
    }
}
